class Solution {
public:
    int massage(vector<int>& nums) {
        int n = nums.size();
        if (n == 0) return 0;
        vector<int> f(n);
        auto g = f;
        f[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            f[i] = g[i - 1] + nums[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[n - 1], g[n - 1]);
    }
};
class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size();
        return max(nums[0] + dfs(nums, 2, n - 2), dfs(nums, 1, n - 1));
    }
    int dfs(vector<int>& nums, int left, int right)
    {
        if (left > right) return 0;
        int n = nums.size();
        vector<int> f(n);
        vector<int> g(n);
        f[left] = nums[left];
        for (int i = left + 1; i <= right; i++)
        {
            f[i] = g[i - 1] + nums[i];
            g[i] = max(g[i - 1], f[i - 1]);
        }
        return max(g[right], f[right]);
    }
};
class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        const int INF = 10100;
        int arr[INF] = { 0 };
        for (auto e : nums) arr[e] += e;
        vector<int> f(INF);
        auto g = f;
        for (int i = 1; i < INF; i++)
        {
            f[i] = g[i - 1] + arr[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[INF - 1], g[INF - 1]);
    }
};
class Solution {
public:
    int minCost(vector<vector<int>>& cost) {
        int n = cost.size();
        vector<vector<int>> dp(n + 1, vector<int>(3));
        for (int i = 1; i <= n; i++)
        {
            dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + cost[i - 1][0];
            dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + cost[i - 1][1];
            dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + cost[i - 1][2];
        }
        return min(dp[n][0], min(dp[n][1], dp[n][2]));
    }
};